//
//  SaoViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/21.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "SaoViewController.h"

#import <AVFoundation/AVFoundation.h>

#import "PureLayout.h"

@interface SaoViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property(nonatomic, retain)AVCaptureSession *session;
@property(nonatomic, retain)AVCaptureVideoPreviewLayer *layer;
@property(nonatomic, retain)UIView *viewCH;

@end

@implementation SaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self sao];
    
    // Do any additional setup after loading the view.
}
-(void)sao{

    //1.捕捉会话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    self.session = session;
    //2.设置输出(摄像头)
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    [session addInput:input];
    
    //3.设置输出(数据)
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    //设置代理之前必须保证output已经载入到会话中
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [session addOutput:output];
    //设置输入类型必须保证output加入到会话之后来设置 设置可以扫描什么类型AVMetadataObjectTypeQRCode二维码类型
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    
    //4.添加阅读图层
    AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    layer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view.layer addSublayer:layer];
    self.view.layer.backgroundColor = (__bridge CGColorRef _Nullable)([UIColor grayColor]);
    self.layer = layer;
    
    UIView *vi = [[UIView alloc] init];
    [self.view addSubview:vi];
    vi.backgroundColor = [UIColor clearColor];
    vi.layer.borderWidth = 3;
    vi.frame = CGRectMake(self.view.frame.size.width/5, 100, self.view.frame.size.width/1.5, self.view.frame.size.width/1.5);
    vi.tag = 1000;
    
    //5.开始扫描
    [session startRunning];
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{

    NSString *string;
    if (metadataObjects.count > 0) {
        //获取扫描的内容
        AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
        string = object.stringValue;
        //停止会话
        [self.session stopRunning];
        
        //移除预览图层
//        [self.layer removeFromSuperlayer];
    }
    if ([string hasPrefix:@"http://"]) {
        //用自己的浏览器打开扫描结果
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
        [self.layer removeFromSuperlayer];
        UIView *vi =  (UIView *)[self.view viewWithTag:1000];
        
        vi.hidden = YES;
    }else{
    
        [self.layer removeFromSuperlayer];
        UIView *vi = (UIView *)[self.view viewWithTag:1000];
        vi.hidden = YES;
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"搜索不到" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            vi.hidden = NO;
            self.tabBarController.tabBar.hidden = NO;
            [self.navigationController popToRootViewControllerAnimated:YES];/////////
            
        }];
        
        [alert addAction:otherAction];
        [self presentViewController:alert animated:YES completion:nil];
    }


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
